﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BenchmarkDotNet.Running;

namespace WPFByCoreApp
{
    /// <summary>
    /// TestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TestWindow : Window
    {
        object a = new object();
        object b = new object();

        public TestWindow()
        {
            InitializeComponent();
        }

        private void btnTestDecimal_Click(object sender, RoutedEventArgs e)
        {
            tb1.Text = TestDecimal.Test();
        }

        private void btnTestDeadLock_Click(object sender, RoutedEventArgs e)
        {
            #region 测试dump文件，检测死锁
            ThreadPool.QueueUserWorkItem(new WaitCallback(par =>
            {

                while (true)
                {
                    lock (a)
                    {
                        lock (b)
                        {
                            System.Diagnostics.Debug.WriteLine("1");
                        }
                    }

                    Thread.Sleep(1);
                }

            }));


            ThreadPool.QueueUserWorkItem(new WaitCallback(par =>
            {
                while (true)
                {
                    lock (b)
                    {
                        lock (a)
                        {
                            System.Diagnostics.Debug.WriteLine("2");
                        }
                    }
                    Thread.Sleep(1000);
                }

            }));
            #endregion
        }

        private void btnTestGC_OnClick(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                for (int i = 100 - 1; i >= 0; i--)
                {
                    byte[] buffer = new byte[1024 * 1024 * 1024];
                    Thread.Sleep(1000);
                }

                GC.Collect(0);
            });
        }

        private void BtnTestBenchmark_OnClick(object sender, RoutedEventArgs e)
        {
            var boKar = BenchmarkRunner.Run<Foo>();

        }
    }
}
